//
//  RecommedViewController.m
//  MilitaryDemo
//
//  Created by qianfeng01 on 15-7-4.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "RecommedViewController.h"
#import "AdView.h"
#import "RecommedModel.h"
#import "FirstCell.h"
#import "SecondCell.h"
#import "UIImageView+WebCache.h"
#import "NewsDetailViewController.h"

@interface RecommedViewController () <UIScrollViewDelegate>
@property(nonatomic,strong)AdView *adView;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSMutableArray *textArr;
@property(nonatomic,strong)NSMutableArray *imageNames;
@property(nonatomic,strong)NSMutableArray *idArr;
@end

@implementation RecommedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textArr=[[NSMutableArray alloc]init];
    self.imageNames=[[NSMutableArray alloc]init];
    self.idArr=[[NSMutableArray alloc]init];
    [self firstDownloadWithID:1];
    [self creatRefreshViewWithID:1];
    }
-(void)addAdView:(NSMutableArray *)imageNames textArr:(NSMutableArray *)textArr
{
    CGRect frame=CGRectMake(2*(kScreenSize.width/6), kScreenSize.height/4-30, self.view.bounds.size.width, 30);
    self.pageControl=[[UIPageControl alloc]initWithFrame:frame];
    self.pageControl.numberOfPages=5;
    self.pageControl.backgroundColor = [UIColor clearColor];
    [self.pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged];
    
    [self.tableView addSubview:self.pageControl];
    self.adView=[[AdView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width,kScreenSize.height/4)];
    self.adView.delegate=self;
    self.adView.showsHorizontalScrollIndicator=NO;
    self.adView.imageNames = imageNames;
    self.adView.textArr=textArr;
    UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] init];
        ;
     [tap addTarget:self action:@selector(tap:)];
    [self.adView addGestureRecognizer:tap];
    self.tableView.tableHeaderView = self.adView;
    
}
-(void)tap:(UITapGestureRecognizer *)tap
{
    
    UIScrollView *view=(UIScrollView *)tap.view;
    NewsDetailViewController *detailController=[[NewsDetailViewController alloc]init];
    int a=view.contentOffset.x/kScreenSize.width;
    detailController.id=self.idArr[a];
    detailController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:detailController animated:YES];
}
- (void)pageControlClick:(UIPageControl *)pageControl {
    // 根据pageControl当前的页码，用代码滚动scrollView，以显示相对应的图片
    NSInteger currentPage = pageControl.currentPage;
    [self.adView setContentOffset:CGPointMake(currentPage*self.view.frame.size.width, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = self.adView.contentOffset.x /self.adView.frame.size.width;
}

-(void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof (self) weakSelf=self;
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            if (self.currentPage == 1) {
                [self.dataArr removeAllObjects];
            }
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *NewsListArr=dict[@"newsList"];
            for (NSDictionary *newsDict in NewsListArr) {
                RecommedModel *model=[[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:newsDict];
                [weakSelf.dataArr addObject:model];
            }
            NSArray *picsListArr=dict[@"picsList"];
         
            for (NSDictionary *picsDict in picsListArr) {
                [weakSelf.textArr addObject:picsDict[@"title"]];
                [weakSelf.imageNames addObject:picsDict[@"img"]];
                [weakSelf.idArr addObject:picsDict[@"id"]];
            }
            [weakSelf addAdView:weakSelf.imageNames textArr:weakSelf.textArr];
        }
        [weakSelf.tableView reloadData];
        [weakSelf endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载推荐数据出错");
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
